package com.helmlabs.grouper.cms.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.helmlabs.grouper.cms.model.AbstractLayout;
import com.helmlabs.grouper.cms.model.AbstractModule;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class TemplateUtil {
	
	private static File readTemplateFile(String template){
		Resource resource = new ClassPathResource("/template/main/" + template + ".jsp");
		try {
			return resource.getFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static String render(String template, Map model){
		Configuration cfg = new Configuration();
		Resource resource = new ClassPathResource("/template/main/");
		try {
			cfg.setDirectoryForTemplateLoading(resource.getFile());
			cfg.setObjectWrapper(new DefaultObjectWrapper());
			Template temp = cfg.getTemplate(template + ".ftl");
			OutputStream writer = new ByteArrayOutputStream();
			OutputStreamWriter out = new OutputStreamWriter(writer);
			temp.process(model, out);			
			return writer.toString();			
//			FileInputStream fis = new FileInputStream(file);
//			BufferedReader buf = new BufferedReader(new InputStreamReader(fis));
//			String line = "";
//			while ((line = buf.readLine())!=null ){
//				render.append(line.replace("#TEMPLATE_CONTENT#", content));
//			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	public static String render(AbstractLayout layout, Map properties) {
		StringBuffer string = new StringBuffer();
		return string.toString();
	}
	
	public static String render(AbstractModule module, Map properties) {
		StringBuffer string = new StringBuffer();
		return string.toString();
	}
	
	
}
